def _idx_to_letter(idx):
    if idx >= 52:
        return chr(idx - 52)
    elif idx < 26:
        return chr(idx + ord('A'))
    else:
        return chr(idx - 26 + ord('a'))


def print_sol_to_std(sol):
    for i in range(len(sol)):
        print('M' + str(i + 1) + ':', end='')
        for j in range(len(sol[i])):
            if j + 1 < len(sol[i]):
                print('(%s,%s%d,%d,%d)' % (_idx_to_letter(sol[i][j][0]), _idx_to_letter(sol[i][j][0]), sol[i][j][1] + 1, sol[i][j][2], sol[i][j][3]), end=',')
            else:
                print('(%s,%s%d,%d,%d)' % (_idx_to_letter(sol[i][j][0]), _idx_to_letter(sol[i][j][0]), sol[i][j][1] + 1, sol[i][j][2], sol[i][j][3]), end='')
        print()


def print_sol_to_file(sol, file_path='data.out'):
    f = open(file_path, 'w', encoding='utf-8')
    for i in range(len(sol)):
        f.write('M' + str(i + 1) + ':')
        for j in range(len(sol[i])):
            if j + 1 < len(sol[i]):
                f.write('(%s,%s%d,%d,%d),' % (_idx_to_letter(sol[i][j][0]), _idx_to_letter(sol[i][j][0]), sol[i][j][1] + 1, sol[i][j][2], sol[i][j][3]))
            else:
                f.write('(%s,%s%d,%d,%d)' % (_idx_to_letter(sol[i][j][0]), _idx_to_letter(sol[i][j][0]), sol[i][j][1] + 1, sol[i][j][2], sol[i][j][3]))
        f.write('\n')
